from direct.gui.DirectGui import *
from pandac.PandaModules import TextNode, WindowProperties, Filename
from direct.gui.OnscreenText import OnscreenText
import copy
import sys
from level_editor import LevelEditor
from menu_button import MenuButton
from singleplayer import SinglePlayer
from panda3d.core import Vec2,Point3,Vec3,Vec4
from dialog import Dialog
'''
	Andrew Oberlin
	Date Created: February 28, 2012
'''
class MainMenu:
	'''
	
	'''
	def __init__(self, parent):
		self.parent = parent
		self.models = dict()
		self.loadModels()
		self.parent.accept("aspectRatioChanged", self.__handleAspectChange)
		winprops = WindowProperties()
		winprops.setCursorFilename(Filename.binaryFilename('models/menu/hud/cursor.cur'))
		self.parent.base.win.requestProperties(winprops)

		
	'''
	
	'''
	def loadModels(self):
		self.models['background'] = self.parent.loader.loadModelCopy("models/plane")
		self.backgroundTexture = self.parent.loader.loadTexture("models/menu/main_menu.png")
		self.models['background'].setTexture(self.backgroundTexture)
		self.models['background'].setTransparency(1)
		self.models['background'].setScale(2.67, 1, 2)
		self.models['background'].reparentTo(self.parent.base.aspect2d)
		
		self.models['exit_button'] = MenuButton(self.parent, 'exit', 'models/menu/exit/exit_option_0', 
			sys.exit, Vec3(.75, 0, -0.65), isInterval = True, isMovie = False, text="Exit")
			
		self.models['campaign_editor_button'] = MenuButton(self.parent, 'campaign_editor', 'models/menu/editor/editor_option', 
			self.startEditor, Vec3(.55, 0, 0.3), text="Campaign Editor", numFrames=24, 
			scale=0.75, textPos = (-0.2, 0, .30))
			
		self.models['multiplayer_button'] = MenuButton(self.parent, 'multiplayer', 'models/menu/multiplayer/multiplayer_option', 
			self.startMultiplayer, Vec3(-0.45, 0, -0.4), text="Multiplayer", numFrames=4, fps=2,
			scale=1, textPos = (-0.5, 0, .5))
			
		self.models['singleplayer_button'] = MenuButton(self.parent, 'singleplayer', 'models/menu/singleplayer/singleplayer_option', 
			self.startSinglePlayer, Vec3(-0.7, 0, 0.7), text="Single Player", numFrames=10, fps=5,
			scale=Vec3(1, 1, 0.35), textPos = (0, 0, -0.2))
	'''
		Hides the title screen
	'''
	def hide(self):
		self.parent.soundLoader.stopMusic('menu')
		self.parent.ignore("aspectRatioChanged")
		for model_key in self.models.keys():
			self.models[model_key].hide()
	
	'''
		Shows the main menu
	'''
	def show(self):
		self.parent.soundLoader.playMusic('menu')
		self.parent.accept("aspectRatioChanged", self.__handleAspectChange)
		for model_key in self.models.keys():
			self.models[model_key].show()
		
	'''
		Resizes the title screen to fit the screen
		Also maintains the aspect ratio
		
		Does not work with fullscreen
	'''
	def __handleAspectChange(self):
		props = self.parent.base.win.getProperties()
		curAspect = props.getXSize()*1.0/props.getYSize()
		if (curAspect != self.parent.WIN_WIDTH/self.parent.WIN_HEIGHT):
			curAspect = self.parent.WIN_WIDTH/self.parent.WIN_HEIGHT
			tmp = copy.deepcopy(props)
			tmp.setSize(props.getXSize(), props.getXSize() / curAspect)
			self.parent.base.win.requestProperties(tmp)
		newScale = 2 * curAspect
		self.scale = Vec3(newScale, 1, 2)
		self.models['background'].setScale(self.scale)
		
	def startEditor(self):
		self.hide()
		self.parent.setLevelEditor(LevelEditor(self.parent))
		
	def startSinglePlayer(self):
		self.hide()
		self.parent.setSinglePlayer(SinglePlayer(self.parent))
		
	def startMultiplayer(self):
		dialog = Dialog(self.parent, 'multiplayer_dialog')
		dialog.addTextBox('Sorry! Multiplayer has not been implemented!')	
		dialog.addOkCancel(dialog.hide, only='ok')
		
			
			
